package com.miguo.component;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.miguo.activity.MainActivity;
import com.miguo.adapter.CategoryGridViewAdapter;
import com.miguo.app.AppHelper;
import com.miguo.bean.AdvertBean;

import java.util.List;

/**
 * File Names: CategoryGridLayout
 * Describe:
 * User: LinXiaoZhi(87100854@qq.com)
 * Date: 2015-07-17
 * Time: 10:52
 */

public class CategoryGridView extends GridView implements AdapterView.OnItemClickListener {
    private List<AdvertBean> mCategoryList;
    private int mColumnCount = 4;
    private Context mContext;
    private Activity mActivity;
    private MainActivity mMainActivity;
    private int mVerticalSpacing = 15;

    private CategoryGridViewAdapter CategoryAdapter;

    public CategoryGridView(Context context) {
        super(context);
        init(context);
    }

    public CategoryGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        mActivity = (Activity) context;
        setNumColumns(mColumnCount);
        setVerticalSpacing(mVerticalSpacing);
    }

    public List<AdvertBean> getmCategories(){
        return mCategoryList;
    }

    public void setCategoryList(List<AdvertBean> category) {
        if (category.size() <= 0) {
            setVisibility(View.GONE);
            return;
        }
        setVisibility(View.VISIBLE);
        mCategoryList = category;
        if (category.size() % 4 != 0) {
            mCategoryList.add(new AdvertBean("更多", "-1"));
        }
        if(CategoryAdapter == null){
            CategoryAdapter = new CategoryGridViewAdapter(mContext);
            CategoryAdapter.addAll(mCategoryList);
            setAdapter(CategoryAdapter);
            setOnItemClickListener(this);
        }else{
            CategoryAdapter.addAll(category,true);
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        if (hasWindowFocus) {
            if (CategoryAdapter != null && CategoryAdapter.getCount() >= 1) {
                CategoryAdapter.notifyDataSetChanged();
            }
            return;
        }
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        AdvertBean category = mCategoryList.get(position);
        if ("-1".equals(category.getItem_pic_url())) {
            mMainActivity.setTabsDisplay(3);
            return;
        }
        AppHelper.startActivityByType(mActivity, category);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

    public void setActivity(MainActivity MainActivity) {
        this.mMainActivity = MainActivity;
    }

    public MainActivity getActivity(){
        return this.mMainActivity;
    }
}
